
# cython: language_level=3
import json
import os

from dotenv import load_dotenv
from fastapi import APIRouter, Depends   #与APIRouter有关
from sqlalchemy import select
from sqlalchemy.orm import Session
from app.db.sql import resource_history_data


from app.db.redis.invitation import get_response_plan_from_redis
from app.db.sql.day_ahead_plan import save_dayahead_plan
from app.db.sql.invitation import save_invitations
from app.db.sql.resource import get_real_time_data
from app.db.sql.vpp_plan import save_vpp_plan
from app.db.sql.vpp_price import save_vpp_price
from app.middlewares.redis import get_redis
from app.models.base_line import BaseLine
from app.models.invitation import Invitation
from app.models.mr_line import MrLine
from app.schemas.platform import (
    GetInvitationRequest,
    GetResourceHistoryDataRequest,
    SendRealTimeDataRequest,
    SendReponsePlanRequest,
    GetDayaheadPlanRequest,
)
from common.database import get_db
from common.http import error_response, ok_response, show_response
from common.log import get_logger
from common.redis import MyRedis
from common.time import get_datetime_now
# web界面的接口全部在此标签下
router = APIRouter(tags=["web"])
# ywy
logger = get_logger("server")


@router.post("/show_RealTimeData")
def send_real_time_data(
        payload: SendRealTimeDataRequest,
        db: Session = Depends(get_db),
        redis: MyRedis = Depends(get_redis),
):
    """
    2.3
    展示实时负荷数据
    """
    # if payload.creditCode != CREDIT_CODE:
    #     return error_response(5000, "creditCode不正确")
    ret_data = get_real_time_data(db, payload, payload.page, payload.pageSize, redis)
    return ok_response(ret_data)


@router.post("/show_ResourceHistoryData")
def get_resource_history_data(
        payload: GetResourceHistoryDataRequest, db: Session = Depends(get_db)
):
    """
    2.8
    虚拟电厂平台向负荷聚合平台请求历史负荷数据。

    由虚拟电厂平台调用负荷聚合平台，获取某个的历史负荷数据信息（1分钟间隔）；每次查询时间范围为3天以内；历史数据可以查询1个月内。
    """
    ret = resource_history_data.get_data(
        db, payload.resourceId, payload.startTime, payload.endTime
    )
    return ok_response(ret)